package io.geekidea.boot.wlwarehouse.controller;



import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiCode;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.wlwarehouse.dto.WLWareHouseDto;
import io.geekidea.boot.wlwarehouse.dto.WlWareHouseAddDto;
import io.geekidea.boot.wlwarehouse.dto.WlWareHouseUpdateDto;
import io.geekidea.boot.wlwarehouse.entity.WlWarehouse;
import io.geekidea.boot.wlwarehouse.service.IWlWarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * <p>
 * 物料仓库 前端控制器
 * </p>
 * @author xukang
 * @since 2023-11-20
 */
@RestController
@RequestMapping("/wl-warehouse")
@Tag(name = "物料仓库控制器")
public class WlWarehouseController {
    @Resource
    private IWlWarehouseService warehouseService;


    @PostMapping("/update")
    public ApiResult updateStore(@RequestBody WlWareHouseUpdateDto wareHouseUpdateDto){
        WlWarehouse warehouse=new WlWarehouse();
        BeanUtil.copyProperties(wareHouseUpdateDto,warehouse);
        warehouseService.updateById(warehouse);
        return ApiResult.success("修改物料类型成功",200);
    }

    @PostMapping("/findAll")
    public ApiResult findAll(@RequestBody WLWareHouseDto wlWareHouseDto){
        Page<WlWarehouse> page=new Page<>(wlWareHouseDto.getPage(),wlWareHouseDto.getSize());
        Page<WlWarehouse> pageDto=warehouseService.page(page);
        PageDto<WlWarehouse> pageInfo=new PageDto<>(pageDto.getRecords(),pageDto.getTotal(),pageDto.getSize(),pageDto.getCurrent());
        return ApiResult.success(pageInfo);
    }


//    @PostMapping("/ban")
//    @Operation(summary = "开启/禁用一个仓库")
//    public ApiResult ban(@RequestBody WlWareHouseUpdateDto wlWareHouseUpdateDto){
//        WlWarehouse wlWarehouse = new WlWarehouse();
//        BeanUtils.copyProperties(wlWareHouseUpdateDto,wlWarehouse);
//        boolean result = warehouseService.updateById(wlWarehouse);
//        return result?ApiResult.success("禁用成功",200):ApiResult.fail("操作失败");
//    }

    @PostMapping("/add")
    @Operation(summary = "添加一个仓库")
    public ApiResult addWarehouse(@RequestBody WlWareHouseAddDto wlWareHouseAddDto){
        if(StringUtils.isBlank(wlWareHouseAddDto.getName())) return ApiResult.fail("不可添加空名仓库");
        WlWarehouse wlWarehouse = new WlWarehouse();
        BeanUtils.copyProperties(wlWareHouseAddDto,wlWarehouse);
        boolean save = warehouseService.save(wlWarehouse);
        return save?ApiResult.success("添加成功",200):ApiResult.fail("添加失败");
    }


    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除一个仓库")
    public ApiResult deleteWarehouse(@PathVariable Integer id){
        boolean result = warehouseService.removeById(id);
        return result?ApiResult.result(ApiCode.SUCCESS):ApiResult.fail("删除失败");
    }




}
